package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileInputStream;
import java.awt.Toolkit;

import javax.swing.*;

import model.CiiBay;

/**
 * Het inlog paneel
 * @author Nick
 * @author Dennis
 * @author Marvin
 *
 */
@SuppressWarnings("serial")
public class InlogPaneel extends JPanel {
	private JTextField gebruikersnaam;
	private JPasswordField wachtwoordVeld;
	private JButton ok;
	private JLabel un,pass;
	private CiiBay ciiBay;
	private Image image;

	public InlogPaneel(CiiBay ciiBay){
		this.ciiBay = ciiBay;
		setLayout(new GridBagLayout());
		image = getFileImage("./files/registratie.jpg");
		gebruikersnaam = new JTextField(10);
		gebruikersnaam.setDocument(new MaxAantalKarakters(25));
		un=new JLabel("Gebruikersnaam:  ");
		wachtwoordVeld = new JPasswordField(10);
		wachtwoordVeld.setDocument(new MaxAantalKarakters(25));
		pass= new JLabel("Wachtwoord:  ");
		ok = new JButton("Ok");
		ok.setBackground(Color.orange);
		gebruikersnaam.setSize(100, 20);
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		add(un, c);
		c.gridx = 1;
		add(gebruikersnaam, c);
		c.gridx = 0;
		c.gridy = 1;
		add(pass,c);
		c.gridx = 1;
		add(wachtwoordVeld,c);
		c.gridheight = 4;
		c.gridwidth = 0;
		c.gridy = 6;
		add(ok, c);
		this.setBackground(Color.ORANGE);

		ok.addActionListener(new InlogKnopHandler());
		wachtwoordVeld.addKeyListener(new PassListener());
	}

	/**
	 * Koppelt de OK knop met de desbetreffende methode in Cii-Bay en geeft een melding terug.
	 * @author Dennis
	 */
	class InlogKnopHandler implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			char[] wwoord = wachtwoordVeld.getPassword();
			StringBuffer ww = new StringBuffer();
			for(char c : wwoord){
				ww.append(c);
			}
			String wachtwoord = ww.toString();
			String message = ciiBay.login(gebruikersnaam.getText(), wachtwoord);
			wachtwoordVeld.setText("");
			gebruikersnaam.setText("");

			if(!message.equals("")) {
				if(!message.equals("U bent ingelogd!")) {
					JOptionPane.showMessageDialog(null, message, "Melding",
							JOptionPane.WARNING_MESSAGE);	
				} else {
					
					if(ciiBay.getIngelogtPersoon().getMailbox().aantalOngelezen() > 0){
					message += "\n\n U heeft " + ciiBay.getIngelogtPersoon().getMailbox().aantalOngelezen() + " nieuwe berichten ontvangen.";
					}
	
					JOptionPane.showMessageDialog(null, message, "Melding",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}


	class PassListener implements KeyListener {

		@SuppressWarnings("static-access")
		@Override
		public void keyPressed(KeyEvent arg0) {
			if(arg0.getKeyCode() == arg0.VK_ENTER){
				ok.doClick();
			}
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
		}
	}

	private Image getFileImage(String filePath) {
		try {
			FileInputStream in = new FileInputStream(filePath);
			byte[] b           = new byte[in.available()];

			in.read(b);
			in.close();

			return Toolkit.getDefaultToolkit().createImage(b);
		} catch (Exception ex) {}
		return null;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (image != null) {
			g.drawImage(image, 0, 0, this);
		}
	}
}
